(** 
    addptlist :
    Add all the point in a list of a line or a column 
    in a square of the image's quadrillage
**)
let acces_mat m i j = m.(i).(j)
let addtolist x = function
  | l -> x::l

let addptlist boolcol x y mat pitch =
  let nbpt = [] in
  let i = ref x in
  let j = ref y in
    if not(boolcol) then
      while (!i <= x - pitch) do
	if fst(mat.(!i).(y)) then
	  begin
	    addtolist (!i,y,snd(mat.(!i).(y))) nbpt;
	    i := !i - 1;
	  end
	else 
	  i := !i - 1;
      done
    else 
      while (!j <= y - pitch) do
	if fst(mat.(x).(!j)) then
	  begin
            addtolist (x,!j,snd(mat.(x).(!j))) nbpt;
	    j := !j - 1;
	  end
	else j := !j - 1
      done;
    nbpt

(** 
    counpt :
    Give the number of point in a line or a column
    of the square in a quadrillage
**)
let countpt boolcol x y mat pitch = 
  List.length (addptlist boolcol x y mat pitch)


(** triangulation **)
let triangulation mat pitch =
  let h = Array.length mat - 1 in
  let w = Array.length mat.(0) - 1 in
  let j = ref h in
  let i = ref w in
  while (!j >= 0) do
    while (!i >= 0) do
      let lpt1 = addptlist true !i !j mat pitch in
      let lpt2 = addptlist false !i !j mat pitch in
      let lpt3 = addptlist false !i (!j-pitch) mat pitch in
      let lpt4 = addptlist true (!i-pitch) !j mat pitch in
      if (((List.length lpt1) = 4) &&
	  ((List.length lpt2) = 4) &&
	  ((List.length lpt3) = 4) &&
	  ((List.length lpt4) = 4)
	 ) then
	print_string ("test reussie")
      else 
	print_int (List.length lpt1);
	print_int (List.length lpt2);
	print_int (List.length lpt3);
	print_int (List.length lpt4);
	print_string ("\n");
      i := !i - pitch;
    done;
    i := w;
      j := !j - pitch;
  done

let mat = Array.make_matrix 10 10 (false,0) in
  begin
    mat.(0).(0) <- (true, 42);
    mat.(9).(9) <- (true, 42);
    mat.(9).(0) <- (true, 42);
    mat.(0).(9) <- (true, 42);
    triangulation mat 1;
  end
